package edu.zut.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


public class CalculateServlet extends HttpServlet {

    //映射的是Post请求
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    //映射的是Get请求
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //http://localhost:8080/Web207_208/calculate?num_1=789&num_2=4567
        //获取请求参数  属于请求中的url,url又属于request对象所有
        //从request对象中解析请求参数,默认获取的数据是字符串类型
        //getParameter : 根据请求参数的name属性获取唯一的value值
        String num_1 = request.getParameter("num_1");
        String num_2 = request.getParameter("num_2");
        System.out.println("num_1 = " + num_1);
        System.out.println("num_2 = " + num_2);
        int sum = Integer.parseInt(num_1) + Integer.parseInt(num_2);
        //调用response对象，获取一个输出流，将求和结果返回给浏览器
        PrintWriter writer = response.getWriter();
        writer.write(num_1 + "+" + num_2 + "=" + sum);
        writer.flush();
        writer.close();

    }
}
